﻿/*******************************************************************************
 * author : Kimm King <br> 
 * datetime : 2008年8月19日16:40:54 <br>
 * description : 系统入口点<br>
 * 
 * 1、定义了整体页面的布局（tree，tab）<br> 
 * 2、预定义了一些信息（treenode）<br> 
 * 3、定义了几个通用方法(popWin) <br>
 */

/*******************************************************************************
 * 全局变量
 ******************************************************************************/

// 生成标签页
var tab = new Ext.TabPanel( {
	id :'tabView',
	region :'center',
	deferredRender :false,
	activeTab :0,
	resizeTabs :true, // turn on tab resizing
	minTabWidth :115,
	tabWidth :135,
	sortable :true,
	enableTabScroll :true
});

/*******************************************************************************
 * 创建系统界面
 ******************************************************************************/
Ext.onReady( function() {

	System.init();

});

/*******************************************************************************
 * // 构建主系统
 ******************************************************************************/
var System = {};
System.CurrentUser = {};

System.fn = function() {
};
System.modules = {};
System.register = function(modulename, module) {
	System.modules[modulename] = module;
};
System.nosupport = function(modulename) {
	Ext.Msg.alert('系统提示', '【' + modulename + '】功能暂不提供！');
};

System.pagingBar = function(store) {
	return new Ext.PagingToolbar( {
		pageSize :10,
		store :store,
		displayInfo :true,
		displayMsg :'显示第 {0} 条到 {1} 条记录，一共 {2} 条',
		emptyMsg :"暂时没有记录"
	});
};

System.init = function() {
	Ext.QuickTips.init();
	Ext.form.Field.prototype.msgTarget = 'side';
	
	var date = new Date();
	//var dateStr = date.format('Y');
	
	//初始化一些变量 
	var aWeek=['星期天','星期一','星期二','星期三','星期四','星期五','星期六'];
	var timeInfoDiv = document.getElementById('timeInfo');
	timeInfoDiv.innerHTML ="<font size=2 color=white><b>" + new Date().format('Y-m-d H:i:s') + '&nbsp;&nbsp;' + aWeek[new Date().getDay()] + "</b></font>";
	//定期更新时间
	Ext.TaskMgr.start({
		run: function(){
			timeInfoDiv.innerHTML ="<font size=2 color=white><b>" +  new Date().format('Y-m-d H:i:s') + '&nbsp;&nbsp;' + aWeek[new Date().getDay()] + "</b></font>";
		}, interval: 1000
	});

	var viewport = new Ext.Viewport(
			{
				layout :'border',
				items : [
						new Ext.BoxComponent( {
							region :'north',
							el :'north',
							height :80
						}),
						new Ext.BoxComponent( {
							region :'south',
							el :'south',
							height :25
						}),
						{
							region :'west',
							id :'west-panel',
							split :true,
							width :200,
							minSize :175,
							maxSize :400,
							margins :'0 0 0 0',
							layout :'accordion',
							title :'系统菜单',
							collapsible :true,
							layoutConfig : {
								animate :true
							},
							items : [
									{
										title :'物流企业管理',
										border :false,
										html :'<div id="manage" style="overflow:auto;width:100%;height:100%"></div>'
									// iconCls:'nav'
									},
									{
										title :'在线帮助',
										border :false,
										// iconCls:'settings',
										html :'<div id="onlinehelp" style="overflow:auto;width:100%;height:100%"></div>'
									} ]
						}, tab // 初始标签页
				]
			});


	var Tree = Ext.tree;
	
	// -----------------------------------------------------------------
	// 1. 功能模块
	// -----------------------------------------------------------------
	var root = {
		draggable :false,
		nodeType: 'async',
		id :'root'
	}
	
	var tree = new Tree.TreePanel( {
		autoScroll :true,
		root :root,
		animate :true,
		enableDD :false,
		border :false,
		rootVisible :false,
		containerScroll :true,
	    dataUrl:extjsbase.rnd('./js/treedata.json?')
	});

	tree.on("click", System.treeClick);
	
	tree.render("manage");
	
	// -----------------------------------------------------------------
	// 2. 在线帮助
	// -----------------------------------------------------------------
	var root_help = new Tree.TreeNode( {
		text :'root help',
		draggable :false,
		//nodeType: 'async',
		id :'rootHelp'
	});
	
	var t00 = new Ext.tree.TreeNode( {
		id :'usermanage',
		leaf :true,
		cls :'file',
		text :'用户管理'
	});
	root_help.appendChild(t00);

	var t01 = new Ext.tree.TreeNode( {
		id :'testmultifile',
		leaf :true,
		cls :'file',
		text :'测试多文件上传'
	});
	root_help.appendChild(t01);
	
	var tree_help = new Tree.TreePanel( {
		//el :'tree-test',
		autoScroll :true,
		root :root_help,
		animate :true,
		enableDD :false,
		border :false,
		rootVisible :false,
		containerScroll :true,
		listeners : {}
	});

	tree_help.on("click", System.treeClick);

	tree_help.render("onlinehelp");

	// end loading
	setTimeout( function() {
		Ext.get('loading').remove();
		Ext.get('loading-mask').fadeOut( {
			remove :true
		});
	}, 250);
	
	// 获取用户登录信息
//	 Ext.Ajax.request({
//	        url:extjsbase.url+ '/security/userInfo',
//	        method:'POST',
//	        success:function(response, options){
//	            var respText = Ext.util.JSON.decode(response.responseText);
//	            System.CurrentUser =respText.message;
//	            if(!System.CurrentUser) return ;
//	            // 显示用户信息
//	            var userInfoSpan = document.getElementById('userInfo');
//	            var dp = System.CurrentUser.departmentName||"";
//				var tempStr = "xxx:"+dp+":"+ System.CurrentUser.userName;
//					userInfoSpan.innerHTML = '<font size="2" color="white"><b>'+tempStr+'</b></font>';
//	          // System.CurrentUser.otypename
//	        }
//	    });
	 
};

// ****************************************************
// 设置树的点击事件
// ****************************************************
System.treeClick = function(node, e) {
	if (node.isLeaf()) {
		var pnode = node;
		var pid = pnode.attributes.code || pnode.id;
		e.stopEvent();

		if (!System.modules[pid]) {
			System.nosupport(pnode.text);
			return;
		}
		
		var n = tab.findById(pid + "");
		if (!n) {
			n = tab.add( {
						id :pid,
						title :String.format('{0}', pnode.text),
						closable :true,
						listeners: { beforeclose: function(){ System.modules[pid].initial = false; } }, 
						html :'<div align="left" vlign="top" id="form_' + pid + '" style="width: 100%;height:100%"></div>'
					});

			
			n.on("activate", function(){
				if(! System.modules[pid].initial)
					System.modules[pid].init();
				System.modules[pid].initial = true;
                if(System.grids[pid]){
                    var activite = Ext.getCmp(System.grids[pid]);
                    activite.setWidth(tab.getInnerWidth());
                    activite.setHeight(tab.getInnerHeight());
                }
			});

			tab.setActiveTab(n);

		} else {
			n.setTitle(String.format('{0}', pnode.text));
			tab.setActiveTab(n);
		}
	}else{
        if(node.isExpanded()){
            node.collapse();
        }else{
            node.expand();
        }
    }
};

/*******************************************************************************
 * *
 * 
 * @param {}
 *            config popWin 弹出居中窗口模板
 ******************************************************************************/

function popWin(config) {

	extjsbase.createDiv();

	var win = {
		el : 'win',
		// title : title,
		layout : 'fit',
		width : 400,
		height : 340,
		closeAction : 'close',
		plain : true,
		modal : true
			// items : ppanel
	};

	Ext.apply(win, config || {});
	new Ext.Window(win).show();
}

/**
 * 修改密码
 */
function upd_pwd_click() {
	//弹出窗口修改密码
    var panel = extjsbase.forms.create('index_upd_pwd',{url:extjsbase.url+'/user/mpwd'});
    popWin( {
        title : '修改密码',
        items : panel,
        width:350,
        height:160
    });
}

/**
 * 关于
 */
var aboutWindow;
function about_click() {
	if (!aboutWindow) {
		var date = new Date();
		var dateStr = date.format('Y');
		aboutWindow = new Ext.Window({
					title : '关于系统',
					width : 400,
					height : 300,
					layout : 'form',
					constrain : true,
					constrainHeader : true,
					maximizable : false,
					minimizable : false,
					closable : false,
					animateTarget: Ext.getBody(),
					modal : true,
					border : false,
					closeAction : 'hide',
					html : '<div style="width: 100%;">'
						 + '<br><p align="center"><font size="5" face="微软雅黑">XXXXXXXXXX平台</font>&nbsp;v1.0</p>'
						 + '<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' 
						 + '<b><font face="Courier New">T&nbsp;&nbsp;E&nbsp;&nbsp;L:</b>&nbsp;&nbsp;(86-21)12345678' 
						 + '<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' 
						 + '<b>E&nbsp;-mail:</b>&nbsp;&nbsp;kimmking@163.com' 
						 + '<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' 
						 + '<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' 
						 + '<b>Q&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Q:</b>&nbsp;&nbsp;632244434' 
						 + '<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' 
						 + '<b>Website:</b>&nbsp;&nbsp;blog.csdn.net/kimmking</font>' 
						 + '<br><br><br><p align="center"><b>XXXXXX科技有限公司</b></p>'
						 + '<br><p align="center">Copyright&copy' + dateStr
						 + '&nbsp;&nbsp;kimmking. All Rights Reserved</p></div>',
					buttons : [{
								text : '确定',
								tooltip : '确定',
								handler : function() {
									aboutWindow.hide();
								}
							}]

				});
	}
	aboutWindow.show();
}

/**
 * 退出
 */
function logout_click() {
	Ext.MessageBox.confirm("XXX平台","确定退出系统平台吗?", function(e) {
        if(e != "yes") return;
				Ext.Ajax.request({
							url : extjsbase.url+ '/security/logout',
							method : 'get',
							success : function(response, action) {
                                var respText = Ext.util.JSON.decode(response.responseText);

								window.location.href = "login.jsp";
							},
							failure : function(response, options) {
                               // Ext.Msg.alert('系统提示',respText.message);
								window.location.href = "login.jsp";
							}
						});
			});
}

/*******************************************************************************
 * 功能模块部分
 ******************************************************************************/
var XXXX = {};
XXXX.version = 1.0;
